package com.dyh.quickdemozwei;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter@Getter
public class WrTest1 {

    int i=0;





    @AllArgsConstructor
    class WR implements Runnable{

        private long time;

        @Override
        public void run() {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            i++;
            log.info("write {} w={}",Thread.currentThread().getName(),i);
        }
    }

    @AllArgsConstructor
    class RR implements Runnable{

        private long time;

        @Override
        public void run() {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("read {} i={}",Thread.currentThread().getName(),i);
        }
    }

    @AllArgsConstructor
    class RR2 implements Runnable{

        private long time1;
        private long time2;

        @Override
        public void run() {
           new RR(time1).run();
           new RR(time2).run();
        }
    }


    @AllArgsConstructor
    class RX implements Runnable{

        private long time;

        @SneakyThrows
        @Override
        public void run() {
           for (;;){
               Thread.sleep(time);
               if (i!=0)
                   break;
           }
           log.info("RX end");
        }
    }


    public static void main(String[] args) throws InterruptedException {
        WrTest1 wt1 = new WrTest1();


        Thread w = new Thread( wt1.new WR(100L)); w.setName("w");
        Thread r1 = new Thread( wt1.new RR(10L)); r1.setName("r1");
        Thread r2 = new Thread( wt1.new RR(200L)); r2.setName("r2");
        Thread r3 = new Thread( wt1.new RR2(10L,159L)); r3.setName("r3");
        Thread r4 = new Thread( wt1.new RX(20)); r4.setName("r4");

        w.start();
        r1.start();
        r2.start();
        r3.start();
        r4.start();

        r1.join();
        r2.join();
        r3.join();
        r4.join();
        System.out.println();
    }
}
